package com.stormragetech.teamwork.dubbo;

import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.common.model.ReturnMsg;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.common.util.AESCrypt;
import com.stormragetech.teamwork.common.util.RSACrypt;
import com.stormragetech.teamwork.member.api.TwUserDubboService;
import com.stormragetech.teamwork.member.model.LoginReqModel;
import com.stormragetech.teamwork.member.model.SignupReqModel;
import com.stormragetech.teamwork.spring.service.TwUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
@Component
public class TwUserDubboServiceImpl implements TwUserDubboService {

    @Value("${aeskey}")
    private String aeskey;

    private final TwUserService twUserService;

    @Autowired
    public TwUserDubboServiceImpl(TwUserService twUserService) {
        Assert.notNull(twUserService, "TwUserService cannot be null !!!");
        this.twUserService = twUserService;
    }

    @Override
    public ReturnMsg signup(final SignupReqModel model) {

        try {

            String pass = AESCrypt.decrypt(model.getPassword(), aeskey);

            try {
                String source = model.getUsername().trim().toUpperCase() +
                        pass.trim().toUpperCase() +
                        model.getRealname().trim().toUpperCase();

                if (!RSACrypt.verify(source, model.getSign(), Constants.PUB_KEY)) {
                    throw new StormragetechException(Constants.CRYPT_ERR_CODE, "验签失败");
                }
            } catch (Exception e) {
                throw new StormragetechException(Constants.CRYPT_ERR_CODE, "验签异常", e);
            }

            twUserService.signup(model.getUsername(), pass, model.getRealname());
            return new ReturnMsg(Constants.RETURN_SUCCESS_CODE, "注册成功");

        } catch (StormragetechException e) {
            return new ReturnMsg(Constants.RETURN_FAILED_CODE, e.getMessage());
        }
    }

    @Override
    public ReturnMsg login(final LoginReqModel model) {

        try {

            String pass = AESCrypt.decrypt(model.getPassword(), aeskey);

            try {
                String source = model.getUsername().trim().toUpperCase() +
                        pass.trim().toUpperCase();

                if (!RSACrypt.verify(source, model.getSign(), Constants.PUB_KEY)) {
                    throw new StormragetechException(Constants.CRYPT_ERR_CODE, "验签失败");
                }
            } catch (Exception e) {
                throw new StormragetechException(Constants.CRYPT_ERR_CODE, "验签异常", e);
            }

            twUserService.login(model.getUsername(), pass);
            return new ReturnMsg(Constants.RETURN_SUCCESS_CODE, "登录成功");

        } catch (StormragetechException e) {
            return new ReturnMsg(Constants.RETURN_FAILED_CODE, e.getMessage());
        }
    }
}